<?php 
function	gestSuppr($_POST, $idtab)
{
	$sql = new db();
	if (isset($_POST['Check']))
	{
		foreach($_POST['Check'] as $value)
		{
			$req = "DELETE FROM ".$idtab." WHERE ".$_POST['id']."=".$value;
			$sql->query($req);
		}
		if ($idtab == "client" || $idtab == "entrepot" || $idtab == "usine" || $idtab == "transporteur")
			{
				foreach($_POST['Check'] as $value)
				{
					$req = "DELETE FROM identification WHERE GLN=".$value;
					$sql->query($req);
				}
			}
	}
	header("Location:".ABS_PATH.ROOT."?page=Admin/Gestion&fct=Admin/Gestion&idtab=".$idtab);
}

function	gestFormu($_POST, $_GET)
{
	$sql = new db();
	if (isset($_GET['val']))
		$req = "SELECT * FROM ".$_GET['idtab']." WHERE `".$_GET['id']."`='".$_GET['val']."'";
	else
		$req = "SELECT * FROM ".$_GET['idtab'];
	$sql->query($req);
	$res = $sql->fetch_array();
	return ($res);
}

function	gestValid($_POST, $_GET)
{
	$i = 0;
	$set = "";
	$tab = array_keys($_POST);
	foreach($_POST as $title => $buff)
		{
			$set .= "`".$title."`='".$buff."', ";
			$i++;
		}
	$set = substr($set, 0, -2);
	$req = "UPDATE ".$_GET['idtab']." SET ".$set." WHERE `".$tab[0]."`='".$_GET['val']."'";
	$sql = new db();
	$sql->query($req);
	header("Location:".ABS_PATH.ROOT."?page=Admin/Gestion&fct=Admin/Gestion&idtab=".$_GET['idtab']);
}

function	gestValAjout($_POST, $_GET)
{
	$values = "";
	foreach($_POST as $buff)
		{
			$values .= "'".$buff."', ";
		}
	$values = substr($values, 0, -2);
	$req = "INSERT INTO ".$_GET['idtab']." VALUES (".$values.")";
	$sql = new db();
	$sql->query($req);
	header("Location:".ABS_PATH.ROOT."?page=Admin/Gestion&fct=Admin/Gestion&idtab=".$_GET['idtab']);
}

function valInscr($_GET)
{
	$sql = new db();
	$req = "UPDATE identification SET Statut = 'V' WHERE GLN = ".$_GET['id'];
	$sql->query($req);
	header("Location:".ABS_PATH.ROOT."?page=Admin/Gestion&fct=Admin/Gestion&idtab=identification");
}

if ($_GET['action'] == "delete")
	gestSuppr($_POST, $_GET['idtab']);
else if ($_GET['action'] == "editer" || $_GET['action'] == "ajouter")
	$res = gestFormu($_POST, $_GET);
else if ($_GET['action'] == "valider")
	gestValid($_POST, $_GET);
else if ($_GET['action'] == "valajout")
	gestValAjout($_POST, $_GET);
else if ($_GET['action'] == "valinscr")
	valInscr($_GET);
?>